﻿using AutoMapper;
using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.IRepository;
using JuCheap.Core.IService.AppService;
using JuCheap.Core.Model;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JuCheap.Core.Service.AppService
{
    /// <summary>
    /// 产品分类扩展服务
    /// </summary>
    public class ProductCategoryExtService : IProductCategoryExtService
    {
        private readonly IProductCategoryRepository _productCategroyRepository;

        /// <summary>
        /// ctor
        /// </summary>
        public ProductCategoryExtService(IProductCategoryRepository productCategroyRepository)
        {
            _productCategroyRepository = productCategroyRepository;
        }

        /// <summary>
        /// 加载一级产品分类树状结构
        /// </summary>
        /// <returns></returns>
        public async Task<List<ProductCategoryDto>> GetTreeAsync(bool justLoadFirstLevel, string parentId)
        {
            var query = from cate in _productCategroyRepository.Queryable()
                        select new ProductCategoryDto
                        {
                            Id = cate.Id,
                            ParentId = cate.ParentId,
                            Sort = cate.Sort,
                            CategoryName = cate.CategoryName
                        };
            query = query.WhereIf(justLoadFirstLevel, x => x.ParentId == "0")
                .WhereIf(parentId.IsNotBlank(), x => x.ParentId == parentId);
            return await query.ToListAsync();
        }

        /// <summary>
        /// 加载菜单表格树状结构
        /// </summary>
        public async Task<List<ProductCategoryDto>> GetTableTreeAsync(ProductCategoryFilter filter)
        {
            var query = from cate in _productCategroyRepository.Queryable()
                        select new ProductCategoryDto
                        {
                            Id = cate.Id,
                            ParentId = cate.ParentId,
                            CategoryName = cate.CategoryName,
                            IsShowInHome = cate.IsShowInHome,
                            IsPublish = cate.IsPublish,
                            Sort = cate.Sort,
                            ImageUrlBig = cate.ImageUrlBig,
                            ImageUrlSmall = cate.ImageUrlSmall,
                            CreateTime = cate.CreateTime
                        };
            query = query.WhereIf(filter.CategoryName.IsNotBlank(), x => x.CategoryName.Contains(filter.CategoryName));
            var list = await query.ToListAsync();
            return DoGetTableTreeChildren(string.Empty, list);
        }

        /// <summary>
        /// 递归获取菜单树
        /// </summary>
        /// <param name="parentId">父级id</param>
        /// <param name="list">菜单原始列表</param>
        /// <returns></returns>
        private List<ProductCategoryDto> DoGetTableTreeChildren(string parentId, IEnumerable<ProductCategoryDto> list)
        {
            var children = list
                .WhereIf(parentId.IsBlank(), x => x.ParentId == "0")
                .WhereIf(parentId.IsNotBlank(), x => x.ParentId == parentId)
                .OrderByDescending(x => x.Sort)
                .ToList();
            foreach (var tree in children)
            {
                tree.Children = DoGetTableTreeChildren(tree.Id, list);
                if (!tree.Children.AnyOne())
                {
                    tree.Children = null;
                }
            }
            return children;
        }
    }
}
